import { Injectable } from '@nestjs/common';
import { CookieOptions } from 'express';
import { ConfigService } from '@nestjs/config';
import { AllConfigType } from '../config/config.type';
import { IAuth } from '@/types';
@Injectable()
export class CookieService {
  authName: string;
  constructor(private config: ConfigService<AllConfigType>) {
    this.authName = this.config.getOrThrow('auth.authName', { infer: true });
  }
  clearCookie(res: IAuth.IResponse) {
    res.clearCookie(this.authName);
  }
  setCookie(res: IAuth.IResponse, token: string, config?: CookieOptions) {
    res.cookie(this.authName, token, config ?? {});
  }
  getCookie(req: IAuth.IRequest) {
    return req.cookies[this.authName];
  }
}
